package com.example.service;

//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Dormitory;
import com.example.entity.Student;
import com.example.mapper.DormitoryMapper;
import com.example.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DormitoryService  {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    private boolean existsInDb(Dormitory dormitory) {
        List<Dormitory> dormitorList = dormitoryMapper.findAllByfloor(dormitory.getFloor_num()
                ,dormitory.getLayer(),dormitory.getRoom_num());
        return !dormitorList.isEmpty();
    }

    public int save(Dormitory dormitory){
        if(existsInDb(dormitory) ) {
            return dormitoryMapper.update(dormitory);
        }else {
            return dormitoryMapper.insert(dormitory);
        }
    }

    public Map<String, Object> saveDormitory(Dormitory dormitory) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save(dormitory);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

    public Map<String, Object> deleteDormitory(String floor_num, Integer layer, Integer room_num) {
        Map<String, Object> result = new HashMap<>();

        Integer deleteResult = dormitoryMapper.deleteBy(floor_num,layer,room_num);

        if (deleteResult != null) {
            result.put("code", 200);
            result.put("message", "删除成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "删除失败");
            result.put("data", null);
        }

        return result;
    }
}
